﻿@model ApprovePath

@{
    int appPrcId = Convert.ToInt32(ViewBag.AppPrcId);
    Layout = "~/Views/Shared/_LayoutEmpty.cshtml";

    var context = new GERPContext(true);
    var positions = from pos in context.Positions.OrderBy(o => o.PosNo).ToList()
                    where pos.RecordStatus != "D"
                    select pos;
    var execPos = from exec in context.ExecutivePositions.ToList()
                  where exec.RecordStatus == "U"
                  select exec;
    var empHeads = from emp in context.Employees.Include(o => o.Position).ToList()
                   where emp.RecordStatus != "D"
                         && emp.Position.ExecPosId == 251
                   select emp;
    var approverTypeSli = new SelectListItem[]
        {
            new SelectListItem{Text = "หัวหน้าสำนัก", Value = "0"},
            new SelectListItem{Text = "ตำแหน่ง", Value = "1"},
            //new SelectListItem{Text = "กลุ่มตำแหน่ง", Value = "2"},
            new SelectListItem{Text = "ตำแหน่งการบริหาร", Value = "3"},
            new SelectListItem{Text = "ตำแหน่งของเจ้าหน้าที่ที่นำไปใช้", Value = "5"},
            new SelectListItem{Text = "ตำแหน่งของเจ้าหน้าที่ที่เกี่ยวข้อง", Value = "6"}
        } ;
} 

@using(Html.BeginForm())
{
    @Html.HiddenFor(m=>m.AppPathId)
    @Html.HiddenFor(m=>m.AppPrcId) 
    @Html.HiddenFor(m=>m.RecordStatus)
    <h4 class="header"> แก้ไขขั้นตอนการอนุมัติ &nbsp;&nbsp;&nbsp;&nbsp; 
        <a href="@Url.Action("Index", new {appPrcId})">ย้อนกลับ</a> |
        <input type="submit" value="บันทึก" />
    </h4>

    <div class="BlockContent" style="font-size: medium;">
        @Html.ValidationSummary()
        @Html.Hidden("AppPrcId", appPrcId)
        <h4 class="DetailHeader">แก้ไขขั้นตอนการอนุมัติ</h4>
        <div class="detail">
            <table>
                <tr>
                    <td valign="top">
                        <strong>ขั้นตอนที่:	 </strong>
                    </td>
                    <td valign="top">
                        @Html.TextBoxFor(m=>m.StepNo)
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <strong>วิธีอ้างถึงผู้อนุมัติ:  </strong>
                    </td>
                    <td valign="top">
                        <select data-val="true" data-val-required="The ApproverType field is required." 
                                id="ApproverType" name="ApproverType">
                            @foreach(var i in approverTypeSli)
                            {
                                <option value="@i.Value" @(Model != null && 
                                                           Convert.ToString((int)Model.ApproverType) == i.Value ? "selected=\"selected\"" : "")>@i.Text</option>
                            } 
                        </select>
                    </td>
                </tr>
                <tr>
                    <td valign="top"><strong>อนุมัติโดย :	</strong></td>
                    <td valign="top">
                        @Html.DropDownListFor(m=>m.PosId, from p in positions select new SelectListItem{Text = p.PosNo + " - " + p.Name, Value = Convert.ToString(p.PosId)}) 
                        @Html.DropDownListFor(m=>m.ExecPosId, from pg in execPos select new SelectListItem{Text = pg.Name, Value = Convert.ToString(pg.ExecPosId)})
                        <div id="EmpJobSiteHead"  >@Html.Raw(string.Join("<br/>", from eh in empHeads select eh.FullName + " [" + eh.JobSite.Name + "]"))</div>
                        <div id="ExecPosIdDiv"  ></div><br/>
                        
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <strong>สิทธิ์ในการอนุมัติของผู้อนุมัติ	: </strong>
                    </td>
                    <td valign="top">
                        <label>@Html.CheckBoxFor(m=>m.IsForNotice)สำหรับแจ้งเตือนเท่านั้น</label> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
                        @*<label>@Html.CheckBoxFor(m=>m.IsForwardable)ส่งต่อได้</label> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
                        <label>@Html.CheckBoxFor(m=>m.IsRejectable)ตีกลับได้</label> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
                        <label>@Html.CheckBoxFor(m=>m.IsSendBackable)ส่งกลับได้</label>*@
                    </td>
                </tr>
            </table>
        </div>
    </div>
}

@section scripts{
    <script>
        $(document).ready(function () {
            
            function OnApproverTypeChanged() {
                var approverType = $("#ApproverType").val();
                if (approverType == 0) {
                    $('#PosId').hide();
                    $('#EmpJobSiteHead').show();
                    $('#ExecPosId').hide();
                    $('#ExecPosIdDiv').hide();
                }
                if (approverType == 1) {
                    $('#PosId').show();
                    $('#EmpJobSiteHead').hide();
                    $('#ExecPosId').hide();
                    $('#ExecPosIdDiv').hide();
                }
                if (approverType == 3) {
                    $('#PosId').hide();
                    $('#EmpJobSiteHead').hide();
                    $('#ExecPosId').show();
                    $('#ExecPosIdDiv').show();
                }
                if (approverType == 5 || approverType == 6) {
                    $('#PosId').hide();
                    $('#EmpJobSiteHead').hide();
                    $('#ExecPosId').hide();
                    $('#ExecPosIdDiv').hide();
                }
            }

            function OnExecPosIdChange() {
                var execPosId = $("#ExecPosId").val();
                var url = '@Url.Action("GetExecPositionEmployee", "ApprovePath")/' + execPosId;
                $.ajax({
                    dataType: "json",
                    url: url, 
                    success: function (d) {
                        $('#ExecPosIdDiv').html(d.Name);
                    }
                });
            }
            
            $("#ApproverType").change(OnApproverTypeChanged);
            $("#ExecPosId").change(OnExecPosIdChange);
            OnApproverTypeChanged();
            OnExecPosIdChange();
        })
    </script>
}